#include "GameObject.h"
#include "GameObjectManager.h"

#include <iostream>
#include <algorithm>

//#include "glut.h"
//#include <GL/gl.h>


GameObject::GameObject(int x, int y, Type type):
	destroyed(false),
	x(x),
	y(y),
	type(type)
{}


void GameObject::destroy()
{
	destroyed = true;
	GameObjectManager::getInstance()->registerObjectForDeletion(this);
}


bool GameObject::isDestroyed() const
{
	return destroyed;
}


void GameObject::draw() const
{
	setDrawColor();

	drawObject(y, x);
}


void GameObject::setColor(float r, float g, float b) 
{ 
	glColor3f(r,g,b); 
}


void GameObject::drawInMode(GLenum mode, const std::vector<drawPoint>& points) const
{
	glBegin(mode);

	for(unsigned int i = 0; i < points.size(); ++i)
	{
		glVertex3dv(reinterpret_cast<const double*>(&points[i]));
	}
	
	glEnd();
}


void GameObject::drawSquare(int row, int col) const
{
	std::vector<drawPoint> points;

	// Draw a square at (row,col).
	points.push_back(drawPoint(col    , row    , 0));
	points.push_back(drawPoint(col + 1, row    , 0));
	points.push_back(drawPoint(col + 1, row + 1, 0));
	points.push_back(drawPoint(col    , row + 1, 0));

	//drawQuadi(points);
	drawInMode(GL_QUADS, points);
}


void GameObject::drawObject(int row, int col) const
{
	drawSquare(row, col);
}


bool GameObject::canBePassed(const GameObject* gameObject) const
{
	return destroyed || isPassable(gameObject); // destroyed objects don't block anything.
}


bool GameObject::isPassable(const GameObject* gameObject) const
{
	return false;
}


void GameObject::handleCollision(GameObject* gameObject)
{
}


void GameObject::getLocation(int& xCur, int& yCur) const
{
    xCur = x;
    yCur = y;
}


void GameObject::setLocation(const int xNew, const int yNew)
{
    x = xNew;
    y = yNew;
}


GameObject::Type GameObject::getType() const
{
	return type;
}